// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import router from './router'
// 加载 element 组件库
import ElementUI from 'element-ui';
// 加载 element 组件库样式
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';
import './assets/icons' // icon
import Cookies from "js-cookie";
import Pagination from "@/components/Pagination";
import moment from 'moment'
import {getToken,setToken,removeToken} from '@/utils/auth'
import '@/utils/drag'
import {getVersion,BeginUpdate,HasEcgFile,DeleteFiles,DockerAnalyze,DownloadToFile,HttpUploadEcg,DeleteFile,getOsVersion} from '@/utils/browser'
// 引入常量
import CONSTANT from '@/utils/constant'
// 引入本地存储
import {setJsonObjectToLocal,getJsonObjectFromLocal,clearDataFromLocal} from '@/utils/localstorage'

import Print from "@/utils/print";

import socket from '@/utils/mysocket'
Vue.prototype.sendMsg = socket.sendMsg


Vue.use(moment);
Vue.use(Print);
Vue.prototype.moment = moment
// 把常量引入到全局变量中
Vue.prototype.$CONSTANT = CONSTANT
Vue.filter('dateYMDHMSFormat',function(dateStr,pattern='YYYY-MM-DD HH:mm:ss'){
  return moment(dateStr).format(pattern);
})


// 全局注册 element 组件库（就可以使用了）
Vue.use(ElementUI);
// 全局组件挂载
Vue.component('Pagination', Pagination)

// 封装一下自己浏览器为Promise
Vue.prototype.getVersion = getVersion;
Vue.prototype.BeginUpdate = BeginUpdate;
Vue.prototype.HasEcgFile = HasEcgFile;
Vue.prototype.myBrowser = {DeleteFiles,DockerAnalyze,DownloadToFile,HttpUploadEcg,DeleteFile,getOsVersion}
Vue.config.productionTip = false

// 挂载消息通知
Vue.prototype.notifySuccess = function(msg){
  this.$notify({title: '成功', message: msg, type: 'success'});
}
Vue.prototype.notifyWarning = function(msg){
  this.$notify({title: '警告', message: msg, type: 'warning'});
}
Vue.prototype.notifyInfo = function(msg){
  this.$notify.info({title: '消息', message: msg});
}
Vue.prototype.notifyError = function(msg){
  this.$notify.error({title: '错误', message: msg});
}
// 重置表单
Vue.prototype.resetForm = function(refName) {
  if (this.$refs[refName]) {
    this.$refs[refName].resetFields();
  }
}

Vue.prototype.msgSuccess = function (msg) {
  this.$message({ showClose: true, message: msg, type: "success" });
}

Vue.prototype.msgError = function (msg) {
  this.$message({ showClose: true, message: msg, type: "error" });
}

Vue.prototype.msgInfo = function (msg) {
  this.$message.info(msg);
}

// 运行环境
Vue.prototype.basePath=process.env.VUE_APP_BASE_PATH
Vue.prototype.downloadServer=process.env.VUE_APP_DOWNLOAD_SERVER

Vue.prototype.getToken=getToken;
Vue.prototype.setToken=setToken;
Vue.prototype.removeToken=removeToken;

/**
 * 获得Cookies里的内容   没有就把该内容存储起来
 * @param key
 * @param value
 */
Vue.prototype.getDataFromCookie = function getDataFromCookie(key) {
  // 获得localStorage对象
  let storage = window.localStorage;
  // 查看当前路径是否存在该缓存信息
  let value = Cookies.get(key);
  if(value){
    // 格式化成json对象
    let jsonObj = JSON.parse(value);
    // 赋值给当前显示列
    if(jsonObj){
      return jsonObj;
    }
  }
  return value;
}

/**
 * Cookies存储对象
 * @param key
 * @param value
 */
Vue.prototype.setDataToCookies = function setDataToCookies(key,value,expires) {
  // 获得localStorage对象
  let jsonStr = JSON.stringify(value);
  Cookies.set(key, jsonStr, { expires: expires });
}

/**
 * 向本地浏览器的某个对象中存储json数据
 * @param localObject 本地对象  localStorage或者sessionStorage
 * @param key
 * @param value
 */
Vue.prototype.setJsonObjectToLocal = setJsonObjectToLocal
/**
 * 根据key获取本地浏览器某个对象的存储json数据
 * @param localObject  本地对象  localStorage或者sessionStorage
 * @param key
 * @returns {DOMPoint|SVGNumber|string|SVGTransform|SVGLength|SVGPathSeg|any}
 */
Vue.prototype.getJsonObjectFromLocal = getJsonObjectFromLocal
/**
 * 清除本地key对应的数据
 * @param localObject
 * @param key
 */
Vue.prototype.clearDataFromLocal = clearDataFromLocal

/* eslint-disable no-new */

// 全局拦截
router.beforeEach((to, from, next) => {
  console.log('全局拦截');
  console.log(to);
  console.log(from);
  let adminToken = getToken();
  console.log(adminToken,'全局拦截中得adminToken');
  // let userInfo = window.localStorage.getItem('userInfo');
  // console.log(userInfo,'userInfo');
  if (to.meta.title) {
    // 这里设置header部分的title
    document.title =  to.meta.title
  }
  if (to.meta.login) {
    if (!adminToken) {
      next({
        path: '/login'
      })
      return
    }
  }
  next()
})

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
